package com.maxd.service.jifen;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.JiFen;
import com.maxd.respository.jifen.JiFenJpaRepository;
import com.maxd.respository.jifen.JiFenRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 积分的实现类
 */
@Service
public class JiFenServiceImpl implements IJiFenService {
    @Autowired
    private JiFenJpaRepository jiFenJpaRepository;
    @Autowired
    private JiFenRepository jiFenRepository;


    @Override
    public Result findAll() {
        return ResultUtil.success(jiFenJpaRepository.findAll());
    }

    @Override
    public Result saveBody(JiFen jiFen) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        jiFen.setCreateAt(sdf.format(now));
        return ResultUtil.success(jiFenJpaRepository.save(jiFen));
    }

    @Override
    public Result findOne(long id) {
        return ResultUtil.success(jiFenJpaRepository.findById(id).orElse(null));
    }

    @Override
    public Result findList(long id) {
        return ResultUtil.success(jiFenJpaRepository.findAllByUserIdOrderByCreateAtDesc(String.valueOf(id)));
    }

    @Override
    public Result findList1(long id, Integer page,Integer size) {
        Pageable pageable = PageRequest.of(page, size);
        return ResultUtil.success(jiFenJpaRepository.findAllByUserIdOrderByCreateAtDesc(pageable, String.valueOf(id)));
    }

    @Override
    public Result delete(long id) {
        jiFenJpaRepository.deleteById(id);
        return ResultUtil.success();
    }

    @Override
    public Result updateBody(JiFen jiFen) {
        JiFen jiFen1 = jiFenJpaRepository.findById(jiFen.getId()).orElse(null);
        assert jiFen1 != null;
        BeanUtils.copyProperties(jiFen1, jiFen);
        return ResultUtil.success();
    }
}
